// 绑定全局右键菜单
/**
 * 
 * isRoot 是否为管理员账号
 *
 */
import {
    BQGIS_skyBox
} from '../../../static/CIM/BQGIS-SkyBox'

import {
    BQGIS_weather,
    defaultOption,
} from "../../../static/CIM/BQGIS-weather.js";

import {
    ElMessage
} from 'element-plus'

import {
    globalTool,
    globalLayer
} from '../../../static/CIM/BQGIS-globalVar'
export default function (viewer, isRoot) {
    if (!globalTool.eventTarget) {
        globalTool.eventTarget = new mars3d.BaseClass() // viewer 自定义事件实例
    }
    const defaultContextmenuItems = viewer.getDefaultContextMenu()
    defaultContextmenuItems[0] = {
        text: "查看此处坐标",
        show: function (e) {
            return Cesium.defined(e.cartesian)
        },
        callback: (e) => {
            const mpt = mars3d.LngLatPoint.fromCartesian(e.cartesian)
            ElMessage({
                message: `经度：${mpt.lng}，纬度：${mpt.lat}，高度：${mpt.alt}`,
            })
        }
    }
    defaultContextmenuItems[1] = {
        text: "查看当前视角",
        callback: (e) => {
            const mpt = viewer.getCameraView()
            ElMessage({
                message: `经度：${mpt.lng}，纬度：${mpt.lat}，高度：${mpt.alt}，方位角heading：${mpt.heading}，俯仰角pitch：${mpt.pitch}`,
            })
        }
    }
    const signConfigIndex = defaultContextmenuItems.findIndex(item => item.text === '图上标记')
    if (signConfigIndex !== -1) {
        defaultContextmenuItems.splice(signConfigIndex, 1)
    }

    const sceneConfig = defaultContextmenuItems.find(item => item.text === '场景设置')
    if (sceneConfig && sceneConfig.children) {
        let index = sceneConfig.children.findIndex(item => item.text === '关闭星空背景')
        if (index !== -1) {
            sceneConfig.children.splice(index, 1)
        }
        index = sceneConfig.children.findIndex(item => item.text === '场景出图')
        if (index !== -1) {
            sceneConfig.children.splice(index, 1)
        }
        index = sceneConfig.children.findIndex(item => item.text === '开启日照阴影')
        if (index !== -1) {
            sceneConfig.children.splice(index, 1)
        }
        index = sceneConfig.children.findIndex(item => item.text === '关闭日照阴影')
        if (index !== -1) {
            sceneConfig.children.splice(index, 1)
        }
        sceneConfig.children.push({
            text: "切换2D视图",
            show: function () {
                return viewer.scene.mode != Cesium.SceneMode.SCENE2D
            },
            callback: () => {
                viewer.scene.mode = Cesium.SceneMode.SCENE2D
            }
        }, {
            text: "切换3D视图",
            show: function () {
                return viewer.scene.mode != Cesium.SceneMode.SCENE3D
            },
            callback: () => {
                viewer.scene.mode = Cesium.SceneMode.SCENE3D
            }
        }, {
            text: "切换2.5D(哥伦布视图)",
            show: function () {
                return viewer.scene.mode != Cesium.SceneMode.COLUMBUS_VIEW
            },
            callback: () => {
                viewer.scene.mode = Cesium.SceneMode.COLUMBUS_VIEW
            }
        }, {
            text: '地球透明度',
            show: true,
            callback: function (e) {
                globalTool.eventTarget.fire("surfaceTransparency")
            },
        })
    }

    defaultContextmenuItems.push({
        text: "收藏功能",
        show: true,
        children: [{
            text: "下载当前图片",
            show: true,
            callback: () => {
                globalTool.eventTarget.fire("startDownImg")
            }
        }, {
            text: "将当前视图收藏至用户收藏夹",
            show: true,
            callback: () => {
                globalTool.eventTarget.fire("startCollect")
            }
        }, {
            text: "打开收藏夹",
            show: true,
            callback: () => {
                globalTool.eventTarget.fire("openFavorites")
            }
        }]
    })

    const effectsConfig = defaultContextmenuItems.find(item => item.text === '特效效果')
    const filterEffectsArr = ['开启下雨', '关闭下雨', '开启下雪', '关闭下雪', '开启雾天气', '关闭雾天气']
    if (effectsConfig) {
        effectsConfig.children = effectsConfig.children.filter(item => filterEffectsArr.indexOf(item.text) === -1)
    }
    defaultContextmenuItems.push({
        text: "切换天空盒",
        show: true,
        children: [{
            text: "晴天",
            show: true,
            callback: () => {
                BQGIS_skyBox.setSkyBox(BQGIS_skyBox.defaultOption.qingtianSkybox);
            }
        }, {
            text: "晚霞",
            show: true,
            callback: () => {
                BQGIS_skyBox.setSkyBox(BQGIS_skyBox.defaultOption.wanxiaSkybox);
            }
        }, {
            text: "蓝天",
            show: true,
            callback: () => {
                BQGIS_skyBox.setSkyBox(BQGIS_skyBox.defaultOption.lantianSkybox);
            }
        }, {
            text: "默认",
            show: true,
            callback: () => {
                BQGIS_skyBox.setSkyBox();
            }
        }]
    })

    defaultContextmenuItems.push({
        text: "天气",
        show: true,
        children: [{
            text: "开启下雨",
            show: () => {
                const rain = BQGIS_weather.getRainEffect(window.viewer);
                return !rain.enabled
            },
            callback: () => {
                const rain = BQGIS_weather.getRainEffect(window.viewer);
                rain.enabled = true
            }
        }, {
            text: "关闭下雨",
            show: () => {
                const rain = BQGIS_weather.getRainEffect(window.viewer);
                return rain.enabled
            },
            callback: () => {
                const rain = BQGIS_weather.getRainEffect(window.viewer);
                rain.enabled = false
            }
        }, {
            text: "开启下雪",
            show: () => {
                const snow = BQGIS_weather.getSnowEffect(window.viewer);
                return !snow.enabled
            },
            callback: () => {
                const snow = BQGIS_weather.getSnowEffect(window.viewer);
                snow.enabled = true
            }
        }, {
            text: "关闭下雪",
            show: () => {
                const snow = BQGIS_weather.getSnowEffect(window.viewer);
                return snow.enabled
            },
            callback: () => {
                const snow = BQGIS_weather.getSnowEffect(window.viewer);
                snow.enabled = false
            }
        }, {
            text: "开启雾",
            show: () => {
                const fog = BQGIS_weather.getFogEffect(window.viewer)
                return !fog.enabled
            },
            callback: () => {
                const fog = BQGIS_weather.getFogEffect(window.viewer)
                fog.enabled = true
            }
        }, {
            text: "关闭雾",
            show: () => {
                const fog = BQGIS_weather.getFogEffect(window.viewer)
                return fog.enabled
            },
            callback: () => {
                const fog = BQGIS_weather.getFogEffect(window.viewer)
                fog.enabled = false
            }
        }]
    })

    // defaultContextmenuItems.push({
    //     text: '日照分析',
    //     show: function (e) {
    //         return e.target.type === '3dtiles'
    //     },
    //     callback: function (e) {
    //         globalTool.eventTarget.fire('shadows', e)
    //     }
    // })

    defaultContextmenuItems.push({
        text: '水淹模拟',
        show: function (e) {
            return e.target.type === '3dtiles'
        },
        callback: function (e) {
            globalTool.eventTarget.fire('flood', e)
        }
    })

    const requestRenderOffEvent = []
    defaultContextmenuItems.push({
        text: '性能优化',
        show: true,
        children: [{
            text: "3dtiles加载设置",
            show: function (e) {
                return e.target.type === '3dtiles'
            },
            callback: function (e) {
                globalTool.eventTarget.fire('tileLoadingConfig', e)
            }
        },
        {
            text: "渲染设置",
            show: true,
            callback: function (e) {
                globalTool.eventTarget.fire('renderConfig', e)
            }
        }
        ],
    })

    defaultContextmenuItems.unshift({
        text: '编辑',
        show: function (e) {
            return e.target.type === '3dtiles' && !e.layer.isEditing
        },
        callback: function (e) {
            e.layer.once(mars3d.EventType.remove, function (event) { // 当模型被删除时，关闭编辑菜单
                globalTool.eventTarget.fire('endEdit', e.layer.id)
            });
            e.layer.startEditing()
            globalTool.eventTarget.fire('startEditTile', e)
        },
    })
    viewer.bindContextMenu(defaultContextmenuItems)
}